//#pragma once 
#include <stdio.h>
#include <unistd.h>
#include <mysql/mysql.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

MYSQL* Connect_MySql(MYSQL *mysql, const char *host, const char* user, const char *passwd, const char *db)
{
    MYSQL *mysql_ret = mysql_real_connect(mysql, host, user, passwd, db, 0, NULL, 0);
    if(mysql_ret == NULL)
    {
        printf("<p>Connect Mysql Fail.");
        exit(1);
    }
    return mysql_ret;
}

void parse_name_passwd(char *name, char *passwd)
{
    char *data = getenv("CONTENT_LENGTH");
    size_t data_len = atoi(data);    
    char *data_buf = (char*)malloc(sizeof(char)*(data_len+1));    
    assert(data_buf != NULL);    
    memset(data_buf, 0, data_len+1);    
    
    int i;
    for(i=0; data_len>i; ++i)
    {
        data_buf[i] = fgetc(stdin);
    }

    //parse user_naem
    data = data_buf;
    while(*data != '=')
    {
        data++;
    }
    data++;
    i = 0;
    while(*data != '&')
    {
        name[i++] = *data++;
    }
    data++;
    
    //parse user_passwd
    while(*data != '=')
    {
        data++;
    }
    data++;
    i = 0;
    while(*data != '\0')
    {
        passwd[i++] = *data++;
    }
}

void skip_page(const char *path)
{
    FILE *fp = fopen(path, "r");
    if(fp == NULL)
    {
        printf("Skip Page Fail.");
        exit(1);
    }
    
    char buf[100] = {0};
    while(!feof(fp))
    {
        fgets(buf, 100, fp);
        printf("%s", buf);
    }

    fclose(fp);
}



